//
//  HNSH_InviteFriendsViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/28.
//

#import "HNSH_InviteFriendsViewController.h"

//view
#import "HNSH_InviteFriendsCell.h"
#import "HNSH_InviteFriendsBottomView.h"
#import "XMShareView.h"

@interface HNSH_InviteFriendsViewController ()
/** */
@property (nonatomic, strong) UIPageControl * pageControl;
/** */
@property (nonatomic, strong) HNSH_InviteFriendsBottomView * bottomView;
/** */
@property (nonatomic, strong) HNSH_InviteFriendsModel * model;
@end

static NSString *HNSH_InviteFriendsCellID = @"HNSH_InviteFriendsCellID";

@implementation HNSH_InviteFriendsViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self showNavTitle:@"邀请好友" showLeftItem:YES];
    [self addAllViews];
    [self configCollectionView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self loadNewData];
}

//MARK: Myself Methods
- (void)addAllViews {
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.pageControl];
    [self.view addSubview:self.bottomView];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionView.mas_bottom).offset(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(76 + kBottomSafeHeight);
    }];
}

- (void)loadNewData{
    [HNSH_UserNetServer requestUserInviteConfigSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            HNSH_InviteFriendsModel * model = [HNSH_InviteFriendsModel yy_modelWithDictionary:responseObject.resultset];
            self.model = model;
            self.pageControl.numberOfPages = self.model.background_urls.count;
                        
            [self.collectionView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];

}

- (void)clickWithTag:(NSInteger)tag{
    if (tag == 0) {
        [self saveImage];
    }
    if (tag == 1) {
        UIImage * img = [self generateScreenshot];
        [XMShareView shareWithTitle:@"" content:@"" url:@"" image:img type:SHARE_ITEM_WEIXIN_SESSION];
    }
    if (tag == 2) {
        UIImage * img = [self generateScreenshot];
        [XMShareView shareWithTitle:@"" content:@"" url:@"" image:img type:SHARE_ITEM_WEIXIN_TIMELINE];
    }
}

- (UIImage *)generateScreenshot{
    UIGraphicsBeginImageContextWithOptions(self.collectionView.bounds.size, 0, [[UIScreen mainScreen] scale]);
    [self.collectionView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

- (void)saveImage{
    UIImageWriteToSavedPhotosAlbum([UIImage createNonInterpolatedUIImageFormCIImage:[UIImage creatQRcodeWithUrlstring:self.model.invite_url] withSize:200], self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (!error) {
        [self.view makeToast:@"保存成功"];
    } else {
        [self.view makeToast:[NSString stringWithFormat:@"%@:%@", @"保存失败", error.localizedDescription]];
    }
}

//MARK: Getter Methods

- (UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(self.collectionView.frame), kScreenWidth - 32, 40)];
        _pageControl.tintColor = [UIColor appGrayLabelColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor appGreenColor];
        _pageControl.currentPage = 0;
    }
    return _pageControl;
}

- (HNSH_InviteFriendsBottomView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[HNSH_InviteFriendsBottomView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kNavBarAndStatusBarHeight - 76 - kBottomSafeHeight, kScreenWidth, 76 + kBottomSafeHeight)];
        _bottomView.backgroundColor = [UIColor whiteColor];
        
        WeakSelf(weakSelf);;
        _bottomView.clickBlock = ^(NSInteger tag) {
            [weakSelf clickWithTag:tag];
        };
    }
    return _bottomView;
}

- (void)configCollectionView {
    UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.collectionViewLayout = layout;
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(kNavBarAndStatusBarHeight, 0, kBottomSafeHeight + 76 + 40 + 16, 0));
    }];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;
    [self.collectionView registerClass:[HNSH_InviteFriendsCell class] forCellWithReuseIdentifier:HNSH_InviteFriendsCellID];
}

//MARK: UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat x = scrollView.contentOffset.x;
    NSInteger index = x / kScreenWidth;
    self.pageControl.currentPage = index;
}

//MARK: UICollectionViewDelegate Methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.background_urls.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HNSH_InviteFriendsCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_InviteFriendsCellID forIndexPath:indexPath];
    cell.imgString = [self.model.background_urls objectAtIndex:indexPath.row];
    cell.model = self.model;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kScreenWidth, kScreenHeight-kNavBarAndStatusBarHeight-kBottomSafeHeight-76-40-16);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
}

@end
